<?php
/**
 * CreateTime: 2024/1/3 16:25
 * User:LinQ
 */

namespace App\Http\Controllers\System;

use App\ApiCall\Wx\Pay\WxPayExecute;
use App\ApiCall\Wx\Qy\WxQyExecute;
use App\Enum\GoodsEnum;
use App\Func\Office\ExcelFunc;
use App\Func\Order\OrderFunc;
use App\Func\System\EnumFunc;
use App\Func\User\UserFunc;
use App\Func\Wx\QyWxLib\WXBizMsgCrypt;
use App\Func\Wx\WxQyFunc;
use App\Models\Users\UsersModel;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

class TestController
{
    public function test(){

        dd((new UsersModel())->where('user_id',15)->first()->toArray());
        // 创建一个新的Excel对象
        // 假设工资表数据存储在名为 $salaryData 的数组中
        $salaryData = [
            [
                "staff_id" => 2084,
                "name" => "徐世辉",
                "id_number" => "331023200105205319",
                "wage" => "4500.00",
                "endowment" => "356.96",
                "medical" => "89.24",
                "unemployment" => "22.31",
                "accumulation" => "0.00",
                "tax_exempt" => "0.00",
                "commercial_health" => "0.00",
                "annuity" => "0.00",
                "tax_deferred" => "0.00",
                "donate" => "0.00",
                "deduction" => "0.00",
                "others" => "0.00",
                "remark" => "",
                "personal_tax" => "0.00",
                "real" => "0.00"
            ], [
                "staff_id" => 2085,
                "name" => "李礼阳",
                "id_number" => "362531198608243310",
                "wage" => "4500.00",
                "endowment" => "356.96",
                "medical" => "89.24",
                "unemployment" => "22.31",
                "accumulation" => "0.00",
                "tax_exempt" => "0.00",
                "commercial_health" => "0.00",
                "annuity" => "0.00",
                "tax_deferred" => "0.00",
                "donate" => "0.00",
                "deduction" => "0.00",
                "others" => "42.92",
                "remark" => "社保补交",
                "personal_tax" => "0.00",
                "real" => "0.00"
            ]
        ];
        // 创建一个新的Excel对象
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        // 合并表头单元格并居中加粗
        $headerInfo = "A1:U2";
        $sheet->mergeCells($headerInfo);
        $style = [
            'alignment' => [
                'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
                'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
            ],
            'font' => [
                'size' => 16,
                'bold' => true
            ]
        ];
        $sheet->getStyle($headerInfo)->applyFromArray($style);
        $sheet->setCellValue('A1', '工资表');
        // 副标题 左对齐
        $leftLow1HeaderInfo = "A3:F3";
        $leftLow2HeaderInfo = "A4:F4";
        $leftStyle = [
            'alignment' => [
                'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT,
                'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
            ]
        ];

        $sheet->mergeCells($leftLow1HeaderInfo)->getStyle($leftLow1HeaderInfo)->applyFromArray($leftStyle);
        $sheet->mergeCells($leftLow2HeaderInfo)->getStyle($leftLow2HeaderInfo)->applyFromArray($leftStyle);


        // 副标题 右对齐
        $right1lowHeaderInfo = "P3:U3";
        $right2lowHeaderInfo = "P4:U4";
        $rightStyle = [
            'alignment' => [
                'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT,
                'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
            ]
        ];

        $sheet->mergeCells($right2lowHeaderInfo)->getStyle($right2lowHeaderInfo)->applyFromArray($rightStyle);
        $sheet->mergeCells($right1lowHeaderInfo)->getStyle($right1lowHeaderInfo)->applyFromArray($rightStyle);
        // 设置外置标题行

        $sheet->setCellValue('A3', '企业名称xx：');
        $sheet->setCellValue('A4', '税款所属期1：2024年01月');
        $sheet->setCellValue('P3', '税号：91330108079303011L');
        $sheet->setCellValue('P4', '金融单位：人民币元（列至角分）');

        // 设置内置标题行
        $normalStyle = [
            'alignment' => [
                'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
                'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
            ]
        ];
        // 合并
        $doubleMergeCellArr = ['A','B','C','D','E','F','G','S','T','U'];
        foreach($doubleMergeCellArr as $value){
            $sheet->mergeCells("{$value}5:{$value}6")->getStyle("{$value}5:{$value}6")->applyFromArray($normalStyle);
        }
        $sheet->setCellValue('A5', '序号');
        $sheet->setCellValue('B5', '姓名');
        $sheet->setCellValue('C5', '工号');
        $sheet->setCellValue('D5', '证件号码');
        $sheet->setCellValue('E5', '工资总额');
        $sheet->setCellValue('F5', '应扣项目');
        $sheet->setCellValue('G5', '应发工资');
        // 处理特殊合并
        $sheet->mergeCells("H5:R5")->getStyle("H5:R5")->applyFromArray($normalStyle);
        $sheet->setCellValue('H5', '扣除项');
        $sheet->setCellValue('H6', '个人养老保险');
        $sheet->setCellValue('I6', '个人医疗险');
        $sheet->setCellValue('J6', '个人失业险');
        $sheet->setCellValue('K6', '个人公积金');
        $sheet->setCellValue('L6', '免税收入');
        $sheet->setCellValue('M6', '商业健康险');
        $sheet->setCellValue('N6', '企业/职业年金');
        $sheet->setCellValue('O6', '准予扣除捐款项');
        $sheet->setCellValue('P6', '减免税额额度');
        $sheet->setCellValue('Q6', '其他');
        $sheet->setCellValue('R6', '备注');
        $sheet->setCellValue('S5', '本次个税');
        $sheet->setCellValue('T5', '实发工资');
        $sheet->setCellValue('U5', '员工签名');

        // 假设工资表数据存储在名为 $salaryData 的数组中
        $row = 7;
        $i = 0;
        // 计算合计
        $peopleSum = count($salaryData);
        $wageSum = 0;
        $endowmentSum = 0;
        $medicalSum = 0;
        $unemploymentSum = 0;
        $accumulationSum = 0;
        $tax_exemptSum = 0;
        $commercial_healthSum = 0;
        $annuitySum = 0;
        $donateSum = 0;
        $deductionSum = 0;
        $othersSum = 0;
        $personal_taxSum = 0;
        $realSum = 0;
        foreach ($salaryData as $data) {
            $wageSum += $data['wage'];
            $endowmentSum += $data['endowment'];
            $medicalSum += $data['medical'];
            $unemploymentSum += $data['unemployment'];
            $accumulationSum += $data['accumulation'];
            $tax_exemptSum += $data['tax_exempt'];
            $commercial_healthSum += $data['commercial_health'];
            $annuitySum += $data['annuity'];
            $donateSum += $data['donate'];
            $deductionSum += $data['deduction'];
            $othersSum += $data['others'];
            $personal_taxSum += $data['personal_tax'];
            $realSum += $data['real'];
            $sheet->setCellValue('A' . $row + $i, $i);
            $sheet->setCellValue('B' . $row + $i, $data['name']);
            $sheet->setCellValue('C' . $row + $i,"" );
            $sheet->setCellValue('D' . $row + $i, $data['id_number']);
            $sheet->setCellValue('E' . $row + $i, $data['wage']);
            $sheet->setCellValue('F' . $row + $i, 0.00);
            $sheet->setCellValue('G' . $row + $i, $data['wage']);
            // 特殊处理这里
            $sheet->setCellValue('H' . $row + $i, $data['endowment']);
            $sheet->setCellValue('I' . $row + $i, $data['medical']);
            $sheet->setCellValue('J' . $row + $i, $data['unemployment']);
            $sheet->setCellValue('K' . $row + $i, $data['accumulation']);
            $sheet->setCellValue('L' . $row + $i, $data['tax_exempt']);
            $sheet->setCellValue('M' . $row + $i, $data['commercial_health']);
            $sheet->setCellValue('N' . $row + $i, $data['annuity']);
            $sheet->setCellValue('O' . $row + $i, $data['donate']);
            $sheet->setCellValue('P' . $row + $i, $data['deduction']);
            $sheet->setCellValue('Q' . $row + $i, $data['others']);
            $sheet->setCellValue('R' . $row + $i, $data['remark']);
            $sheet->setCellValue('S' . $row + $i, $data['personal_tax']);
            $sheet->setCellValue('T' . $row + $i, $data['real']);
            $sheet->setCellValue('U' . $row + $i, "");
            $i++;
        }
        //  处理最后一行
        $sheet->setCellValue('A' . $row + $i, "合计");
        $sheet->setCellValue('B' . $row + $i, $peopleSum."人");
        $sheet->setCellValue('C' . $row + $i,"--");
        $sheet->setCellValue('E' . $row + $i,"--");
        $sheet->setCellValue('F' . $row + $i, $wageSum);
        $sheet->setCellValue('G' . $row + $i, 0.00);
        // 特殊处理这里
        $sheet->setCellValue('H' . $row + $i, $endowmentSum);
        $sheet->setCellValue('I' . $row + $i, $medicalSum);
        $sheet->setCellValue('J' . $row + $i, $unemploymentSum);
        $sheet->setCellValue('K' . $row + $i, $accumulationSum);
        $sheet->setCellValue('L' . $row + $i, $tax_exemptSum);
        $sheet->setCellValue('M' . $row + $i, $commercial_healthSum);
        $sheet->setCellValue('N' . $row + $i, $annuitySum);
        $sheet->setCellValue('O' . $row + $i, $donateSum);
        $sheet->setCellValue('P' . $row + $i, $deductionSum);
        $sheet->setCellValue('Q' . $row + $i, $othersSum);
        $sheet->setCellValue('R' . $row + $i, "--");
        $sheet->setCellValue('S' . $row + $i, $personal_taxSum);
        $sheet->setCellValue('T' . $row + $i, $realSum);
        $sheet->setCellValue('U' . $row + $i, "");
        // 处理最右下侧合并
        $sub1 = "R".$row + $i + 1 .":S".$row + $i + 1;
        $sub2 = "T".$row + $i + 1 .":U".$row + $i + 1;
        $sheet->mergeCells($sub1)->getStyle($sub1)->applyFromArray($leftStyle);
        $sheet->mergeCells($sub2)->getStyle($sub2)->applyFromArray($leftStyle);
        $sheet->setCellValue( "R".$row + $i + 1 , '审核人：');
        $sheet->setCellValue( "T".$row + $i + 1, '制单人：');
        $filePath = (new ExcelFunc())->getExcelFromData($spreadsheet,".","excel_test");
        dump($filePath);
        die("ok");
        $data = [];
        dd(json_encode([
            'goods_info' => [
                [
                    'goods_id' => 1,
                    'goods_number' => 1
                ],
                [
                    'goods_id' =>2,
                    'goods_number' => 1
                ],
            ],
            'address_id' => 1,
            'pay_type' => 'wx',
            'sw' => 1,
            'prefix' => 'jsapi'
        ]));
        $data = [];
        try {
            for($x=0;$x<=49;$x++) {
                for ($i = 0; $i <= 49; $i++) {
                    for ($i = 0; $i <= 4; $i++) {
                        $data[] = [
                            "X" => $x,
                            "Y" => $i,
                            "Z" => $i,
                            "Time" => time(),
                            "Value" => [
                                33,
                                142,
                                140
                            ]
                        ];
                    }
                }
            }
            $ison = json_encode($data, JSON_PRETTY_PRINT);
            $filename = 'xdata.json';
            file_put_contents($filename, $ison);
        }catch (\Exception $exception){
            dd("ERROR");
        }

        die("ok");


        $userInfo = (new UserFunc())->getLoginInfo();
        dd($userInfo);

        // TODO 订单
        $orderNo = (new OrderFunc())->createOrder(1,
            2,
            4,
            'default',
            'jsapi');
        dd($orderNo);
        // 202402041707036192376388
        // 12177525012014070332333
        (new WxPayExecute('default'))->native([
            'mchid'        => '1667268842',
            'out_trade_no' => 'native12177525012014070332333',
            'appid'        => 'wxeb1821a1f993f22e',
            'description'  => 'fxqTest',
            'notify_url'   => 'http://mft.iwebg.cn/api/test',
            'amount'       => [
                'total'    => 1,
                'currency' => 'CNY'
            ],
        ]);
    }

    public function upload(){
        dd(1);
        dd((new WxQyFunc())->getAccessToken());
        file_put_contents('xxx.txt',json_encode([1,2,3,4]));
    }

    public function qyWxMessageCallBack(){
        $token = 'NunyFo7brP0';
        $encodingAesKey= 'bCAb8oiImoKQA3NzgoTbJukc4B0pLQgdvdjh9sh7r8e';
        $receiveid = config('openapi.wx.qy_wx.corp_id');;

        $wxcpt = new WXBizMsgCrypt($token, $encodingAesKey, $receiveid);
        $sVerifyMsgSig = $_GET['msg_signature'];
        $sVerifyTimeStamp = $_GET['timestamp'];
        $sVerifyNonce = $_GET['nonce'];
        $sVerifyEchoStr = $_GET['echostr'];
        $sEchoStr = "";
        // call verify function
        $errCode = $wxcpt->VerifyURL($sVerifyMsgSig, $sVerifyTimeStamp, $sVerifyNonce, $sVerifyEchoStr, $sEchoStr);
        file_put_contents("xxxx1.txt",$errCode);
        if ($errCode == 0) {
            echo $sEchoStr . "\n";
        } else {
            print("ERR: " . $errCode . "\n\n");
        }
        echo "";
    }




}
